#include "SipSrvListTrans.hpp"
#include "SipGDBodyAdaptor.hpp"
#include "SipUserMessage.hpp"

#include "SipUserAgent.hpp"
#include "resip/stack/TransactionTerminated.hxx"

#include "rutil/Logger.hxx"
#define RESIPROCATE_SUBSYSTEM Subsystem::APP

namespace wtsip{

CSipSrvListTrans::CSipSrvListTrans(SipMessage& request, CSipUserAgent& ua):
CSipSrvTransaction(request, ua),
m_resp(0), m_beDestroyed(false)
{
    WarningLog(<< "ListTrans new : " << this);
}

CSipSrvListTrans::~CSipSrvListTrans(void)
{
    if (m_resp){
        delete m_resp;
    }
    WarningLog(<< "ListTrans delete : " << this);
}


bool CSipSrvListTrans::send(std::auto_ptr<SipMessage> msg)
{
    /*if (CSipGDBodyAdaptor::listOver(*msg)){
        m_beDestroyed = true;
    }*/
    CSipSrvTransaction::send(msg);
    /*if (m_beDestroyed)
    {
        m_ua.postSelfMessage(new TransactionTerminated(m_tid, false, &m_ua));
    }*/
    return true;
}


bool CSipSrvListTrans::processIncoming(SipMessage* msg)
{
    m_ua.postTimerMessage(new TranTimerMessage(m_tid, false), 5);
    return CSipSrvTransaction::processIncoming(msg);
}


bool CSipSrvListTrans::processTimeOut(ApplicationMessage* msg){
    if (!msg)
        return false;

    m_beDestroyed = true;
    return m_ua.postSelfMessage(new TransactionTerminated(m_tid, false, &m_ua));
}


SipMessage* CSipSrvListTrans::createResponse(int code)
{
    if (!m_resp){
        m_resp = CSipSrvTransaction::createResponse(code);
    }
    return static_cast<SipMessage*>(m_resp->clone());
}


bool CSipSrvListTrans::canDestroy(){
    return m_beDestroyed;
}


}//namespace

